package com.quasar.hpatient.widget;

import android.app.TimePickerDialog;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TimePicker;

import com.quasar.hpatient.R;
import com.quasar.hpatient.utils.TimeZoneUtil;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * author : 许建宏
 * Date: 2017/11/1 0001
 * View:设置时间
 */

public class WidgetTime extends LinearLayout implements View.OnClickListener {

    TextView tv_timeDate;
    private static final int msgKey1 = 1;
    TimeThread timeThread;

    public WidgetTime(Context context) {
        super(context);
        init(context);
    }

    public WidgetTime(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }
    public void init(Context context) {
        LayoutInflater.from(context).inflate(R.layout.widget_time_layout, this);
        tv_timeDate = (TextView) findViewById(R.id.tv_timeDate);
        tv_timeDate.setOnClickListener(this);
        //获取当前时间并服初始值
        FormatTime(TimeZoneUtil.getHour(new Date()), TimeZoneUtil.getMinute(new Date()));
//       new TimeThread().start();
    }

    public void setTextVal(String val) {
        tv_timeDate.setText(val);
    }

    //格式化时间
    public void FormatTime(int hourOfDay, int minute) {
        String min = minute + "";
        if (min.length() == 1) {
            //当分钟只有一位数时前面添加0
            StringBuffer stringBufferMin = new StringBuffer(min);
            stringBufferMin.insert(min.length() - 1, "0").toString();
            tv_timeDate.setText(hourOfDay + ":" + stringBufferMin);
        } else {
            tv_timeDate.setText(hourOfDay + ":" + minute);
        }
    }

    //获取时间
    public String getTime() {
        return tv_timeDate.getText().toString().trim();
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.tv_timeDate:
                TimePickerDialog dialog = new TimePickerDialog(getContext(), new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                        FormatTime(hourOfDay, minute);
                    }
                }, TimeZoneUtil.getHour(new Date()), TimeZoneUtil.getMinute(new Date()), true);
                dialog.show();
                break;
        }
    }


    public class TimeThread extends Thread {
        @Override
        public void run() {
            super.run();
            do {
                try {
                    Thread.sleep(1000);
                    Message msg = new Message();
                    msg.what = msgKey1;
                    mHandler.sendMessage(msg);

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } while (true);
        }
    }

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case msgKey1:
                    long time = System.currentTimeMillis();
                    Date date = new Date(time);
                    SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
                    tv_timeDate.setText(format.format(date));
                    break;
                default:
                    break;
            }
        }
    };

}
